<?php

class FrontController extends BackendController
{


    public $layout='/layouts/column2';
    public $city_en;
    public function actionFlash(){
        Yii::app()->clientScript->registerScriptFile($this->adminPath . '/js/jquery.min.js',CClientScript::POS_HEAD);
        $city_id = Yii::app()->admin->getState('city');
        $info = CityConfig::model()->find('city_id=:city_id and name=:name and type=:type',array(':city_id'=>$city_id,':name'=>'frontFlash',':type'=>'front'));
        if($info){
            $info_array = Xtutuquan::string2array(Xtutuquan::new_stripslashes($info->value));
        }else{
            $info_array = '';
        }
        $this->render('flash',array('info_array'=>$info_array));
    }

    public function actionNav(){
        Yii::app()->clientScript->registerScriptFile($this->adminPath . '/js/jquery.min.js',CClientScript::POS_HEAD);
        $city_id = Yii::app()->admin->getState('city');
        $tabs = Yii::app()->request->getParam('tabs');
        if($tabs==1 || !$tabs){
            $info = CityConfig::model()->find('city_id=:city_id and name=:name and type=:type',array(':city_id'=>$city_id,':name'=>'frontArea',':type'=>'front'));
            if($info){
                $info_array = Xtutuquan::string2array(Xtutuquan::new_stripslashes($info->value));
                $id_array = array();
                foreach($info_array as $v){
                    $id_array[$v['id']] = $v['id'];
                    $title_array[$v['id']] = $v['title'];
                    $order_array[$v['id']] = $v['listorder'];
                }
            }else{
                $info_array = '';
                $id_array = '';
                $title_array ='';
                $order_array = '';
            }
            //取出区域数据
            $city_array = CityArea::model()->findAll('city_id=:city_id and status=:status',array(':city_id'=>$city_id,':status'=>1));
            $this->render('nav',array('city_array'=>$city_array,'info_array'=>$info_array,'id_array'=>$id_array,'title_array'=>$title_array,'order_array'=>$order_array));
        }elseif($tabs==2){
            $info = CityConfig::model()->find('city_id=:city_id and name=:name and type=:type',array(':city_id'=>$city_id,':name'=>'frontPlay',':type'=>'front'));
            if($info){
                $info_array = Xtutuquan::string2array(Xtutuquan::new_stripslashes($info->value));
                $id_array = array();
                foreach($info_array as $v){
                    $id_array[$v['id']] = $v['id'];

                    $order_array[$v['id']] = $v['listorder'];
                }
            }else{
                $info_array = '';
                $id_array = '';

                $order_array = '';
            }
            //取出区域数据
            $theme_array = Xtutuquan::zhutiArr();
            $this->render('play',array('theme_array'=>$theme_array,'info_array'=>$info_array,'id_array'=>$id_array,'order_array'=>$order_array));
        }elseif($tabs==3){
            $info = CityConfig::model()->find('city_id=:city_id and name=:name and type=:type',array(':city_id'=>$city_id,':name'=>'frontHot',':type'=>'front'));
            if($info){
                $info_array = Xtutuquan::string2array(Xtutuquan::new_stripslashes($info->value));
                $id_array = array();
                foreach($info_array as $v){
                    $id_array[$v['id']] = $v['id'];
                    $title_array[$v['id']] = $v['title'];
                    $order_array[$v['id']] = $v['listorder'];
                }
            }else{
                $info_array = '';
                $id_array = '';
                $title_array ='';
                $order_array = '';
            }
            //取出推荐的目的地数据
            $address_array = Article::model()->findAll('frond_hot=:frond_hot and city_id=:city_id and is_show=:is_show',array(':frond_hot'=>1,':city_id'=>$city_id,':is_show'=>1));
            $this->render('hot',array('title_array'=>$title_array,'address_array'=>$address_array,'info_array'=>$info_array,'id_array'=>$id_array,'order_array'=>$order_array));
        }
    }
    public function actionUpdateHot(){
        $city_id = Yii::app()->admin->getState('city');
        if(Yii::app()->request->isPostRequest){
            $info = CityConfig::model()->find('city_id=:city_id and name=:name and type=:type',array(':city_id'=>$city_id,':name'=>'frontHot',':type'=>'front'));
            if($info){
                if($_POST['hot']){
                    $array = array();
                    if(count($_POST['hot'])>10){
                        $data['status'] = 0;
                        $data['message'] = '最多配置10个热门地';
                        echo json_encode($data);
                        Yii::app()->end();
                    }
                    foreach($_POST['hot'] as $k=>$v){
                        $array[$k]['id'] = $k;
                        if($_POST['title'][$k]){
                            $array[$k]['title'] = $_POST['title'][$k];
                        }else{
                            $array[$k]['title'] = $v;
                        }
                        $array[$k]['listorder'] = intval($_POST['listorder'][$k]);
                        $array[$k]['type'] = $_POST['type'][$k];
                        $array[$k]['token'] = $_POST['token'][$k];
                    }
                    $sort = array(
                        'direction' => 'SORT_DESC', //排序顺序标志 SORT_DESC 降序；SORT_ASC 升序
                        'field'     => 'listorder',       //排序字段
                    );
                    $sub_arr = Xtutuquan::arrayListorder($array,$sort);
                    $info->value = Xtutuquan::array2string($sub_arr);
                }else{
                    $info->value = '';
                }
                $ok = $info->update();
            }else{
                $db = new CityConfig();
                if($_POST['hot']){
                    $array = array();
                    if(count($_POST['hot'])>10){
                        $data['status'] = 0;
                        $data['message'] = '最多配置10个热门地';
                        echo json_encode($data);
                        Yii::app()->end();
                    }
                    foreach($_POST['hot'] as $k=>$v){
                        $array[$k]['id'] = $k;
                        if($_POST['title'][$k]){
                            $array[$k]['title'] = $_POST['title'][$k];
                        }else{
                            $array[$k]['title'] = $v;
                        }
                        $array[$k]['listorder'] = intval($_POST['listorder'][$k]);
                        $array[$k]['type'] = $_POST['type'][$k];
                        $array[$k]['token'] = $_POST['token'][$k];
                    }
                    $sort = array(
                        'direction' => 'SORT_DESC', //排序顺序标志 SORT_DESC 降序；SORT_ASC 升序
                        'field'     => 'listorder',       //排序字段
                    );
                    $sub_arr = Xtutuquan::arrayListorder($array,$sort);
                    $db->value = Xtutuquan::array2string($sub_arr);
                }else{
                    $db->value = '';
                }
                $db->city_id =$city_id;
                $db->name = 'frontHot';
                $db->type = 'front';
                $db->content = '首页热门地导航';
                $ok = $db->save();
            }
            if($ok){
                $data['status'] = 1;
                $data['message'] = '设置成功';
            }else{
                $data['status'] = 0;
                $data['message'] = '设置失败';
            }
            echo json_encode($data);
            Yii::app()->end();
        }
    }
    public function actionUpdatePlay(){
        $city_id = Yii::app()->admin->getState('city');
        if(Yii::app()->request->isPostRequest){
            $info = CityConfig::model()->find('city_id=:city_id and name=:name and type=:type',array(':city_id'=>$city_id,':name'=>'frontPlay',':type'=>'front'));
            if($info){

                if($_POST['theme']){
                    $array = array();
                    if(count($_POST['theme'])>12){
                        $data['status'] = 0;
                        $data['message'] = '最多配置12个主题';
                        echo json_encode($data);
                        Yii::app()->end();
                    }
                    foreach($_POST['theme'] as $k=>$v){
                        $array[$k]['id'] = $k;
                        $array[$k]['title'] = $v;
                        $array[$k]['listorder'] = intval($_POST['listorder'][$k]);
                    }
                    $sort = array(
                        'direction' => 'SORT_DESC', //排序顺序标志 SORT_DESC 降序；SORT_ASC 升序
                        'field'     => 'listorder',       //排序字段
                    );
                    $sub_arr = Xtutuquan::arrayListorder($array,$sort);
                    $info->value = Xtutuquan::array2string($sub_arr);
                }else{
                    $info->value = '';
                }
                $ok = $info->update();

            }else{
                $db = new CityConfig();
                if($_POST['theme']){
                    $array = array();
                    if(count($_POST['theme'])>12){
                        $data['status'] = 0;
                        $data['message'] = '最多配置12个主题';
                        echo json_encode($data);
                        Yii::app()->end();
                    }
                    foreach($_POST['theme'] as $k=>$v){
                        $array[$k]['id'] = $k;
                        $array[$k]['title'] = $v;
                        $array[$k]['listorder'] = intval($_POST['listorder'][$k]);
                    }
                    $sort = array(
                        'direction' => 'SORT_DESC', //排序顺序标志 SORT_DESC 降序；SORT_ASC 升序
                        'field'     => 'listorder',       //排序字段
                    );
                    $sub_arr = Xtutuquan::arrayListorder($array,$sort);
                    $db->value = Xtutuquan::array2string($sub_arr);
                }else{
                    $db->value = '';
                }
                $db->city_id =$city_id;
                $db->name = 'frontPlay';
                $db->type = 'front';
                $db->content = '首页主题导航';
                $ok = $db->save();
            }
            if($ok){
                $data['status'] = 1;
                $data['message'] = '设置成功';
            }else{
                $data['status'] = 0;
                $data['message'] = '设置失败';
            }
            echo json_encode($data);
            Yii::app()->end();
        }
    }







    public function actionUpdateArea(){
        $city_id = Yii::app()->admin->getState('city');
        if(Yii::app()->request->isPostRequest){
            $info = CityConfig::model()->find('city_id=:city_id and name=:name and type=:type',array(':city_id'=>$city_id,':name'=>'frontArea',':type'=>'front'));
            if($info){
                if($_POST['area']){
                    $array = array();
                    if(count($_POST['area'])>10){
                        $data['status'] = 0;
                        $data['message'] = '最多配置10个区域';
                        echo json_encode($data);
                        Yii::app()->end();
                    }
                    foreach($_POST['area'] as $k=>$v){
                        $array[$k]['id'] = $k;
                        if($_POST['title'][$k]){
                            $array[$k]['title'] = $_POST['title'][$k];
                        }else{
                            $array[$k]['title'] = $v;
                        }
                        $array[$k]['listorder'] = intval($_POST['listorder'][$k]);
                    }
                    $sort = array(
                        'direction' => 'SORT_DESC', //排序顺序标志 SORT_DESC 降序；SORT_ASC 升序
                        'field'     => 'listorder',       //排序字段
                    );
                    $sub_arr = Xtutuquan::arrayListorder($array,$sort);
                    $info->value = Xtutuquan::array2string($sub_arr);
                }else{
                    $info->value = '';
                }
                $ok = $info->update();
            }else{
                $db = new CityConfig();
                if($_POST['area']){
                    $array = array();
                    if(count($_POST['area'])>10){
                        $data['status'] = 0;
                        $data['message'] = '最多配置10个区域';
                        echo json_encode($data);
                        Yii::app()->end();
                    }
                    foreach($_POST['area'] as $k=>$v){
                        $array[$k]['id'] = $k;
                        if($_POST['title'][$k]){
                            $array[$k]['title'] = $_POST['title'][$k];
                        }else{
                            $array[$k]['title'] = $v;
                        }
                        $array[$k]['listorder'] = intval($_POST['listorder'][$k]);
                    }
                    $sort = array(
                        'direction' => 'SORT_DESC', //排序顺序标志 SORT_DESC 降序；SORT_ASC 升序
                        'field'     => 'listorder',       //排序字段
                    );
                    $sub_arr = Xtutuquan::arrayListorder($array,$sort);
                    $db->value = Xtutuquan::array2string($sub_arr);
                }else{
                    $db->value = '';
                }
                $db->city_id =$city_id;
                $db->name = 'frontArea';
                $db->type = 'front';
                $db->content = '首页区域导航';
                $ok = $db->save();
            }
            if($ok){
                $data['status'] = 1;
                $data['message'] = '设置成功';
            }else{
                $data['status'] = 0;
                $data['message'] = '设置失败';
            }
            echo json_encode($data);
            Yii::app()->end();
        }
    }



    public function actionFlashAdd(){
        $city_id = Yii::app()->admin->getState('city');
        if(Yii::app()->request->isPostRequest){
            $_POST['Flash']['title'] = CHtml::encode($_POST['Flash']['title']);
            $_POST['Flash']['url'] = CHtml::encode($_POST['Flash']['url']);
            $_POST['Flash']['listorder'] = intval($_POST['Flash']['listorder']);
            if(!$_POST['thumb']){
                $data['status'] = 0;
                $data['message'] = '请上传幻灯片';
                echo json_encode($data);
                Yii::app()->end();
            }else{
                $_POST['Flash']['thumb'] = $_POST['thumb'];
            }
            $array[] = $_POST['Flash'];
            //获取城市配置表  flash字段
            $info = CityConfig::model()->find('city_id=:city_id and name=:name and type=:type',array(':city_id'=>$city_id,':name'=>'frontFlash',':type'=>'front'));
            if($info){ //如果有  则合并数组  按照 listorder重新排列
               $info_array = Xtutuquan::string2array(Xtutuquan::new_stripslashes($info->value));
               $sub_arr = CMap::mergeArray($info_array,$array);
                $sort = array(
                    'direction' => 'SORT_DESC', //排序顺序标志 SORT_DESC 降序；SORT_ASC 升序
                    'field'     => 'listorder',       //排序字段
                );
                $sub_arr = Xtutuquan::arrayListorder($sub_arr,$sort);
                $info->value = Xtutuquan::array2string($sub_arr);
                $ok = $info->update();
            }else{
               //木有 //如果没有 则写入 数组
               $sub_arr = $array;
               $db = new CityConfig();
               $db->value = Xtutuquan::array2string($sub_arr);
               $db->city_id =$city_id;
               $db->name = 'frontFlash';
               $db->type = 'front';
               $db->content = '首页幻灯片';
               $ok = $db->save();
            }
            if($ok){
                $data['status'] = 1;
                $data['message'] = '加幻灯片成功';
            }else{
                $data['status'] = 0;
                $data['message'] = '添加幻灯片失败';
            }
            echo json_encode($data);
            Yii::app()->end();
        }else{
            Yii::app()->clientScript->registerScriptFile($this->adminPath . '/js/jquery.min.js',CClientScript::POS_HEAD);
            $this->layout = '/layouts/dialog';
            $this->render('flash_add');
        }
    }
    public function actionFlashDelete(){
        $id = intval(Yii::app()->request->getParam('id'));
        $city_id = Yii::app()->admin->getState('city');
        $info = CityConfig::model()->find('city_id=:city_id and name=:name and type=:type',array(':city_id'=>$city_id,':name'=>'frontFlash',':type'=>'front'));
        if($info){
            $info_array = Xtutuquan::string2array(Xtutuquan::new_stripslashes($info->value));
            unset($info_array[$id]);
            array_values($info_array);
            $info->value = Xtutuquan::array2string($info_array);
            $ok = $info->update();
            $data['status'] = 1;
            $data['message'] = '删除数据成功';
        }else{
            $data['status'] = 0;
            $data['message'] = '你无权作此操作';
        }
        echo json_encode($data);
        Yii::app()->end();
    }
    public function actionFlashUpdate(){
        $id = intval(Yii::app()->request->getParam('id'));
        $city_id = Yii::app()->admin->getState('city');
        $info = CityConfig::model()->find('city_id=:city_id and name=:name and type=:type',array(':city_id'=>$city_id,':name'=>'frontFlash',':type'=>'front'));
        if($info){
            Yii::app()->clientScript->registerScriptFile($this->adminPath . '/js/jquery.min.js',CClientScript::POS_HEAD);
            if(Yii::app()->request->isPostRequest){
                    $info_array = Xtutuquan::string2array(Xtutuquan::new_stripslashes($info->value));
                    $info_array[$id]['title'] = CHtml::encode($_POST['Flash']['title']);
                    $info_array[$id]['url'] = CHtml::encode($_POST['Flash']['url']);
                    $info_array[$id]['listorder'] = intval($_POST['Flash']['listorder']);
                    $sort = array(
                            'direction' => 'SORT_DESC', //排序顺序标志 SORT_DESC 降序；SORT_ASC 升序
                            'field'     => 'listorder',       //排序字段
                        );
                    $sub_arr = Xtutuquan::arrayListorder($info_array,$sort);
                    $info->value = Xtutuquan::array2string($sub_arr);
                    $ok = $info->update();
                if($ok){
                    $data['status'] = 1;
                    $data['message'] = '修改幻灯片成功';
                }else{
                    $data['status'] = 0;
                    $data['message'] = '修改幻灯片失败';
                }
                echo json_encode($data);
                Yii::app()->end();
            }
            $this->layout = '/layouts/dialog';
            $info_array = Xtutuquan::string2array(Xtutuquan::new_stripslashes($info->value));
            $dd = $info_array[$id];
            $this->render('flash_update',array('info'=>$dd,'id'=>$id));
        }else{
            exit('你无权作此操作');
        }
    }

}